﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DestinationOperations;

namespace Beginning
{
    public partial class frmMain : Form
    {
        private DataGridViewCellCollection current;

        public frmMain()
        {
            InitializeComponent();
            BindControls();
            BusesBindControls();
        }

        private void BindControls()
        {
            var data = from d in DestinationOperations.DAO.GetAllDestinations()
                       select new
                       {
                           ID = d.DestinationID,
                           Destination = d.DestistionName
                       };

            dgvMain.DataSource = data.ToList();
        }

        private void BusesBindControls()
        {
            var data = from b in DAO.GetAllBuses()
                       select new
                       {
                           ID = b.BusID,
                           RegNum = b.BusRegNumber,
                           Capacity = b.BusCapacity
                       };

            dgvBuses.DataSource = data.ToList();
        }

        private void btnAddDestinationn_Click(object sender, EventArgs e)
        {
            AddDestination frmAdd = new AddDestination();
            frmAdd.Show();
            this.Hide();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            current = dgvMain.CurrentRow.Cells;
            frmDestinationDetails frmDetails = new frmDestinationDetails(
                Convert.ToInt64(current["ID"].Value.ToString()));
            frmDetails.Show();
            this.Hide();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            current = dgvMain.CurrentRow.Cells;

            long id = Convert.ToInt64(current["ID"].Value.ToString());

            bool confirm =
                MessageBox.Show("Delete the selected destination?", "Confirmation", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question) == DialogResult.Yes;

            if (id != 0)
            {
                if (confirm)
                {
                    DestinationOperations.DAO.DeleteDestination(id);
                }
            }

            BindControls();
        }

        private void btnAddBus_Click(object sender, EventArgs e)
        {
            gbAddNewBus.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            gbAddNewBus.Enabled = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Buse newBus = new Buse();
            newBus.BusCapacity = Convert.ToInt32(txtCapacity.Text);
            newBus.BusRegNumber = txtRegNum.Text;

            bool confirm =
                MessageBox.Show("Add this bus?", "Confirmation", MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK;

            if (confirm)
            {
                DAO.AddNewBus(newBus);
                MessageBox.Show("A new Bus Added!");
                BusesBindControls();
                gbAddNewBus.Enabled = false;
            }
        }

        private void btnDeleteBus_Click(object sender, EventArgs e)
        {
            current = dgvBuses.CurrentRow.Cells;

            long id = Convert.ToInt64(current["ID"].Value.ToString());

            if (id != 0)
            {
                bool confirm =
                    MessageBox.Show("Delete this bus?", "Confirmation", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK;

                if (confirm)
                {
                    DAO.DeleteBus(id);
                    MessageBox.Show("The Bus was Deleted!");
                }
            }

            BusesBindControls();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmBusesSchedules busSchedules = new frmBusesSchedules();
            busSchedules.Show();
            this.Hide();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }
    }
}
